
    //
//  WWAreadIdBridge.m
//  WhatWeather
//
//  Created by byn on 15/11/28.
//  Copyright © 2015年 byn. All rights reserved.
//

#import "WWAreadIdBridge.h"
#import "WWAreaInfo.h"

@implementation WWAreadIdBridge

+ (WWAreadIdBridge *)getInstance {
    static WWAreadIdBridge * instance = nil;
    static dispatch_once_t onceWWAreadID;
    dispatch_once(&onceWWAreadID, ^{
        instance = [[WWAreadIdBridge alloc] init];
    });
    return instance;
}
- (instancetype)init {
    if ([super init]) {
        [self initData];
    }
    return self;
}

- (void)initData {
    
    self.searchTextWithPatternMatch = @"SELECT * FROM chinaCity WHERE cityNameCN = ?";
}

- (BOOL)isSearchTextChinese {
    return YES;
}

- (NSArray *)packAreaInfo:(FMResultSet *)resultSet {
    
    NSMutableArray * arAreaID = [[NSMutableArray alloc] init];
    if (nil != resultSet) {
        while ([resultSet next]) {
            [arAreaID addObject:[WWAreaInfo setupAreaInfo:resultSet]];
        }
    }
    
    return arAreaID;
}

- (NSString *)getFinalString : (NSString *)name {
    NSMutableSet *arAreaID = [[NSMutableSet alloc] init];
    NSString *sqlitePath = [[NSBundle mainBundle] pathForResource:@"cityChina" ofType:@"db"];
    
    FMDatabase *db = [FMDatabase databaseWithPath:sqlitePath];
    if (![db open]) {
        return nil;
    }
    
    FMResultSet *s2 = [db executeQuery:@"SELECT cityNameCN FROM chinaCity"];
        //[arAreaID addObjectsFromArray:[self packAreaInfo:s2]];
    NSString *finalName = nil;
    while ([s2 next]) {
        NSString *tempName = [s2 stringForColumn:@"cityNameCN"];
        if ([name rangeOfString:tempName].location != NSNotFound) {
            finalName = tempName;
            break;
        }
    }
    
    [self.db close];
    
    
    return finalName;
}
-(NSArray *)queryDataWithSearchText: (NSMutableString *)searchText searchPattern: (SearchPattern)searchPattern {
    
    NSMutableSet *arAreaID = [[NSMutableSet alloc] init];
    NSString *sqlitePath = [[NSBundle mainBundle] pathForResource:@"cityChina" ofType:@"db"];
    
    self.db = [FMDatabase databaseWithPath:sqlitePath];
    if (![self.db open]) {
        return nil;
    }
    
    
    FMResultSet *s2 = [self.db executeQuery:self.searchTextWithPatternMatch,searchText];
    [arAreaID addObjectsFromArray:[self packAreaInfo:s2]];
    
    [self.db close];
    
    return [arAreaID allObjects];
}

@end










































